package com.rech.xbexpress3;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.rech.utils.OtherUtil;

import java.util.Timer;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class JjResultActivity extends AppCompatActivity {
    private int jjcount = 100;
    private TextView djs ;
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */


    private CountDownTimer mTimer=null; //UI业务倒计时
    private static final boolean AUTO_HIDE = true;
    private ImageView imageView29;

    /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * Some older devices needs a small delay between UI widget updates
     * and a change of the status and navigation bar.
     */
    private static final int UI_ANIMATION_DELAY = 300;
    private final Handler mHideHandler = new Handler();

    private final Runnable mHidePart2Runnable = new Runnable() {
        @SuppressLint("InlinedApi")
        @Override
        public void run() {
            // Delayed removal of status and navigation bar

        }
    };
    private final Runnable mShowPart2Runnable = new Runnable() {
        @Override
        public void run() {
            // Delayed display of UI elements
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.show();
            }
        }
    };
    private boolean mVisible;
    private final Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };
    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (AUTO_HIDE) {
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences("xbbox", Context.MODE_PRIVATE);
        String orientation = sharedPreferences.getString("Orientation","");
        if (orientation.equals("landscape")) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        setContentView(R.layout.activity_jj_result);
        djs = findViewById(R.id.tv_djs);
        mVisible = true;
        initTimer();

        imageView29 = (ImageView)findViewById(R.id.imageView29);
        String cnImage     =  sharedPreferences.getString("CabinetType_gzh","");
        new FetchUrlImageTask(cnImage,imageView29).execute();

        Button jj_result_back = (Button) findViewById(R.id.jj_result_back);
        jj_result_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
                editor.putString("jj_kdysj", "");
                editor.putString("jj_ljm1", "");
                //editor.putString("jj_ljm2", "");
                editor.putString("jj_sjr", "");
                editor.putString("jj_sfz", "");
                editor.putString("jj_name", "");
                editor.putString("jj_sjh", "");
                editor.putInt("jj_exp",0);
                editor.putString("jj_kdysj", "");
                editor.putString("jj_sjr", "");
                editor.putInt("jj_grid",0);
                editor.putString("jj_gridtype","");
                editor.putString("jj_gridnum","");
                editor.apply();
                unInitTimer();
                finish();
                //startActivity(new Intent(FullscreenActivity.this, FaceActivity.class));
            }
        });

        Button jj_result_jx = (Button) findViewById(R.id.jj_result_jx);
        jj_result_jx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor1 = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
                editor1.putString("jj_kdysj", "");
                editor1.putString("jj_ljm1", "");
                //editor1.putString("jj_ljm2", "");
                editor1.putString("jj_sjr", "");
                editor1.apply();
                Intent intent = new Intent();
                setResult(1001, intent);
                finish();
                //startActivity(new Intent(FullscreenActivity.this, FaceActivity.class));
            }
        });

        TextView tv_version = findViewById(R.id.tv_version);
        TextView tv_waiter = findViewById(R.id.tv_waiter);
        try {
            tv_version.setText("v" + OtherUtil.getVersion(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String phone = sharedPreferences.getString("CabinetType_phone","");
        tv_waiter.setText("客服电话:" + phone);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
        delayedHide(100);
    }

    private void toggle() {
        if (mVisible) {
            hide();
        } else {
            show();
        }
    }

    private void hide() {
        // Hide UI first
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        mVisible = false;

        // Schedule a runnable to remove the status and navigation bar after a delay
        mHideHandler.removeCallbacks(mShowPart2Runnable);
        mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
    }

    @SuppressLint("InlinedApi")
    private void show() {
        // Show the system bar
        mVisible = true;

        // Schedule a runnable to display UI elements after a delay
        mHideHandler.removeCallbacks(mHidePart2Runnable);
        mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
    }

    /**
     * Schedules a call to hide() in delay milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
    private void initTimer(){
        if (mTimer == null) {
            mTimer = new CountDownTimer((long) (100 * 1000), 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    jjcount--;
                    djs.setText(String.valueOf(jjcount));
                }

                @Override
                public void onFinish() {

                    SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
                    editor.putString("jj_sfz", "");
                    editor.putString("jj_name", "");
                    editor.putString("jj_sjh", "");
                    editor.putInt("jj_exp",0);
                    editor.putString("jj_kdysj", "");
                    editor.putString("jj_ljm1", "");
                    //editor.putString("jj_ljm2", "");
                    editor.putString("jj_sjr", "");
                    editor.putInt("jj_grid",0);
                    editor.putString("jj_gridtype","");
                    editor.putString("jj_gridnum","");
                    unInitTimer();
                    finish();
                }
            };
            mTimer.start();
        }
    }
    private void unInitTimer(){
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }
}
